/**
 * 
 */
package cc.comac.mom.datahandlers.entity;

import java.util.ArrayList;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;

import cc.comac.mom.bean.EquipPartBorrow;
import cc.comac.mom.bean.EquipPartBorrowBean;
import cc.comac.mom.bean.ResultDev;
import cc.comac.mom.dao.entity.EquipPartBorrowDao;
import cc.comac.mom.entity.EquipPartBorrowEntity;
import cc.comac.mom.util.BaseEntityDataUtil;
import cc.comac.mom.util.MomConstants;
import cc.comac.mom.util.ResultBean;

/**
* @Description: 
* @author: fyt 
* @date: 2020年7月16日下午9:34:16
*/
@Component
@Transactional
public class EquipPartBorrowDataHandler extends ServiceImpl<EquipPartBorrowDao, EquipPartBorrowEntity>{

	@Autowired
	private EquipPartBorrowDao equipPartBorrowDao;
	
//	@Autowired
//	private IdFactory idFactory;
	@Autowired
	private BaseEntityDataUtil 	baseEntityDataUtil;
	
	@Autowired
	private UniversalNumberGenerationDataHandler universalNumberGenerationDataHandler;
	
	public ResultBean save(EquipPartBorrow obj) {
		ResultBean resultBean = new ResultBean(true);
		if(obj == null) {
			throw new RuntimeException("数据为空");
		}
		EquipPartBorrowEntity innerData = obj.getInnerData();
		if(innerData  == null) {
			throw new RuntimeException("数据为空");
		}
		if(innerData.getBorrowTime() == null) {
			throw new RuntimeException("领用日期为空");
		}
		if(innerData.getStore() == null || "".equals(innerData.getStore())) {
			throw new RuntimeException("仓库为空");
		}
		if(innerData.getBorrowType() == null || "".equals(innerData.getBorrowType())) {
			throw new RuntimeException("领用类型为空");
		}
		if(innerData.getBorrowUser() == null || "".equals(innerData.getBorrowUser())) {
			throw new RuntimeException("申请人为空");
		}
		if(innerData.getUserDepartment() == null || "".equals(innerData.getUserDepartment())) {
			throw new RuntimeException("部门为空");
		}
		if(innerData.getIsReturn() <0) {	
			throw new RuntimeException("是否归还为空");
		}
		if(innerData.getIsReturn() == 1) {
			if(innerData.getReturnTime() == null) {
				throw new RuntimeException("归还日期为空");
			}
			if(innerData.getNotifyDay() <0) {	
				throw new RuntimeException("提醒天数为空");
			}
		}
		//领用单号不填，自动生成
		if(innerData.getBorrowOrderNumber() == null || "".equals(innerData.getBorrowOrderNumber())) {
//			innerData.setBorrowOrderNumber(idFactory.generateBorrowNo());
			String numberGeneration = universalNumberGenerationDataHandler.numberGeneration(universalNumberGenerationDataHandler.getBorrowNumberGeneration());
			innerData.setBorrowOrderNumber(numberGeneration);
		}
		//领用确认状态，默认未确认
		innerData.setBorrowConfirm(MomConstants.EQUIP_PART_BORROW_STATUS_UNCONFIRM);
		baseEntityDataUtil.setBaseEntityData(innerData);
		int insert = equipPartBorrowDao.insert(innerData);
		if(insert<1) {
			throw new RuntimeException("新增失败");
		}
		resultBean.setMessage(String.valueOf(innerData.getId()));
		return resultBean;
	}

	public ResultBean update(EquipPartBorrow obj) {
		ResultBean resultBean = new ResultBean(true);
		if(obj == null || obj.getInnerData() == null) {
			throw new RuntimeException("数据为空");
		}
		EquipPartBorrowEntity innerData = obj.getInnerData();
		if(innerData.getBorrowTime() == null) {	
			throw new RuntimeException("领用日期为空");
		}
		if(innerData.getStore() == null || "".equals(innerData.getStore())) {
			throw new RuntimeException("仓库为空");
		}
		if(innerData.getBorrowType() == null || "".equals(innerData.getBorrowType())) {
			throw new RuntimeException("领用类型为空");
		}
		if(innerData.getBorrowUser() == null || "".equals(innerData.getBorrowUser())) {
			throw new RuntimeException("申请人为空");
		}
		if(innerData.getUserDepartment() == null || "".equals(innerData.getUserDepartment())) {
			throw new RuntimeException("部门为空");
		}
		if(innerData.getIsReturn() <0) {
			throw new RuntimeException("是否归还为空");
		}
		if(innerData.getBorrowOrderNumber() == null || "".equals(innerData.getBorrowOrderNumber())) {
			throw new RuntimeException("领用单号为空");
		}
		if(innerData.getBorrowConfirm() == null || "".equals(innerData.getBorrowConfirm())) {	
			throw new RuntimeException("领用确认信息为空");
		}
		baseEntityDataUtil.updateBaseEntityData(innerData);
		int updateById = equipPartBorrowDao.updateById(innerData);
		if(updateById<1) {
			throw new RuntimeException("修改失败");
		}
		return resultBean;
	}
	
	public ResultBean deleteBatch(List<EquipPartBorrow> list) {
		ResultBean resultBean = new ResultBean(true);
		if(list == null || list.size()<1) {
			throw new RuntimeException("数据为空");
		}
		ArrayList<Long> listIds = new ArrayList<Long>();
		
		for(EquipPartBorrow item:list) {
			EquipPartBorrowEntity innerData = item.getInnerData();
			if(innerData == null) {
				throw new RuntimeException("数据为空");	
			}
			listIds.add(innerData.getId());
		}
		List<EquipPartBorrowEntity> selectBatchIdsList = equipPartBorrowDao.selectBatchIds(listIds);
		if(selectBatchIdsList == null || selectBatchIdsList.size()<1) {
			throw new RuntimeException("数据为空");	
		}
		for(EquipPartBorrowEntity item :selectBatchIdsList) {
			baseEntityDataUtil.deleteBaseEntityData(item);
		}
		boolean updateBatchById = updateBatchById(selectBatchIdsList);
		
		if(!updateBatchById) {
			throw new RuntimeException("删除失败");
		}

		return resultBean;
	}	
	
	public ResultBean borrowConfig(List<Long> ids) {
		ResultBean resultBean = new ResultBean(true);
		if(ids == null || ids.size()<1) {
			throw new RuntimeException("数据为空");	
		}
		List<EquipPartBorrowEntity> selectBatchIds = equipPartBorrowDao.selectBatchIds(ids);
		if(selectBatchIds == null || selectBatchIds.size()<1) {
			throw new RuntimeException("数据为空");
		}
		selectBatchIds.forEach(item -> {
			item.setBorrowConfirm(MomConstants.EQUIP_PART_BORROW_STATUS_CONFIRM);
			baseEntityDataUtil.updateBaseEntityData(item);
		});
		boolean updateBatch = this.updateBatchById(selectBatchIds);
		if(!updateBatch) {
			throw new RuntimeException("确认失败");
		}
		return resultBean;
	}
	
	public ResultDev selectByFields(EquipPartBorrowBean obj) {
		ResultDev resultDev = new ResultDev();
		if (obj.getPageSize() == 0) {
			obj.setPageSize(10);
		}
		obj.setPageNum((obj.getPageNum()-1)*obj.getPageSize());
		List<EquipPartBorrowBean> listEquipPartBorrowBean = equipPartBorrowDao.selectByFields(obj);
		long countByFields = equipPartBorrowDao.selectCountByFields(obj);
		
		// 将查询总数除以每页数量后向上取整获取总页数
		double pageCount = Math.ceil((double)countByFields / obj.getPageSize());
		resultDev.setEntityList(listEquipPartBorrowBean);
		resultDev.setPageCount((int)pageCount);
		resultDev.setTotalQuantity((int)countByFields);
		return resultDev;
	}
}
